home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10592 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  96 lines

  1. Path: news.platinum.com!dcmark
  2. From: dcmark@platinum.com (Mark Juric)
  3. Newsgroups: comp.lang.c++,comp.os.linux.development.apps,comp.os.linux.misc
  4. Subject: Are str* functions okay in C++?
  5. Date: 8 Mar 1996 18:50:40 GMT
  6. Organization: PLATINUM technology, Inc.
  7. Message-ID: <4hpvi0$gt6@news.platinum.com>
  8. NNTP-Posting-Host: ns2-ssn.platinum.com
  9.  
  10. Hi all,
  11.   I am relatively new to C++ and I'm having a heck of a time porting a C
  12. program to C++.  I have a function that opens a man page and tries to
  13. determine if it points to another man page.  I use strtok and strcpy in the
  14. function.  It works fine under Solaris 2.x, however, under Linux 1.2.x or
  15. 1.3.x it chokes on a delete[] statement after several hundred iterations.
  16. Here's the function.  It dies under Linux on the delete[] path; statement.
  17.  
  18. g++: 2.7.2
  19. libg++: 2.7.1
  20.  
  21. on all platforms.  Is this a problem with my code, or is something else
  22. going on here?
  23.  
  24. Notes:  mbuf[MAXLINE] is definately big enough 
  25.     *entry is definately big enough    
  26.  
  27.  
  28. // Check if the first line is an alias.  If so, get directory
  29. void
  30. Index::check_alias(char **entry,
  31.            char *f,
  32.            const char *dirname,
  33.            const char *fullpath)
  34. {
  35.   char mbuf[MAXLINE];
  36.   char *alias, *ta, *path, *path2, *section, *tptr, *comm_name;;
  37.   char *filename = new char[strlen(f)+1];
  38.   static int num = 0;
  39.  
  40.   alias = ta = path = path2 = section = tptr = comm_name = 0;
  41.   
  42.   strcpy(filename,f);
  43.   sprintf(mbuf,"%s/%s/%s",fullpath,dirname,f);
  44.   
  45.   ifstream infile(mbuf);
  46.   if(!infile) error("Can't open manpage",mbuf);
  47.   infile.getline(mbuf,MAXLINE);
  48.  
  49.   alias = new char[strlen(mbuf) + 1];
  50.   strcpy(alias,mbuf);
  51.   
  52.   comm_name = strtok(filename,".");
  53.  
  54.   cout << "alias: " << alias << "\n";
  55.   if((tptr = strstr(alias,".so")) != NULL){
  56.     tptr += strlen(".so ");            // Gets, for instance, 'man2/utime.2'
  57.     path = new char[strlen(fullpath) + strlen(tptr) + 2];
  58.  
  59.     sprintf(path,"%s/%s",fullpath,tptr);
  60.     section = strtok(alias,"/");
  61.     section = §ion[strlen(section)-1];
  62.   }
  63.   else{
  64.     sprintf(mbuf,"%s/%s/%s",fullpath,dirname,f);
  65.     path = new char[strlen(mbuf)+1];
  66.  
  67.     strcpy(path,mbuf);
  68.     section = (char *)&dirname[strlen(dirname)-1];
  69.   }
  70.   
  71.   
  72.   // Allocate for comm_name, section, path, 2 spaces and \0
  73.  
  74.   // Need to find an elegant way to check for existance before calling strlen
  75.   path2 = new char[strlen(comm_name) + strlen(section) + strlen(path) + 3];
  76.  
  77.   sprintf(path2,"%s %s %s",comm_name,section,path);
  78.   delete[] path; 
  79.  
  80.   cout << "path2  " << path2 << " num " << num++ << "\n";
  81.   strcpy(*entry,(char *)path2);
  82.  
  83.  
  84.   delete[] path2;
  85.   delete[] filename;
  86.  
  87. }
  88.  
  89.  
  90. Thanks,
  91.  
  92.  
  93. -Mark
  94. __
  95. juric@platinum.com
  96.